package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.area.SearchAreaBo;
import com.zhuss.bean.vo.core.area.ListAreaVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.AreaDao;
import com.zhuss.core.db.entity.Area;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;


/**
 * @description: 地域读
 * @author: zhushanshuo
 * @create: Thu Apr 08 20:24:46 CST 2021
 **/
@Component("areaRead")
public class AreaRead {
	
	@Autowired
	private AreaDao areaDao;
	
	
	public Map<String, Area> listMap() {
		List<Area> list = areaDao.listAll();
		Map<String, Area> map = new HashMap<>();
		for (Area c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}
	
	/**
	 * 地域
	 * @return
	 */
	public Object listArea() {
		List<Area> areaList = areaDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(areaList, result);
		return result;
	}
	
	
	/**
	 * 地域
	 * @return
	 */
	public Object listArea(List<Long> areaIdList) {
		List<Area> result = areaDao.listById(areaIdList);
		return result;
	}
	
	
	/**
	 * 根据名字查询地域
	 * @param 
	 * @return
	 */
	public List<Area> list(String name) {
		List<Area> result = areaDao.listByName(name);
		return result;
	}
	
	public List<Area> listNotId(String name, Long areaId) {
		List<Area> result = new ArrayList<Area>();
		List<Area> list = list(name);
		for (Area c : list) {
			if (!c.getAreaId().equals(areaId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 地域 
	 * @return
	 */
	public Object listAreaId() {
		List<Area> areaList = areaDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(areaList, result);
		return result;
	}
	
	private void transferListId(List<Area> areaList, List<Long> result) {
		for (Area item : areaList) {
			result.add(item.getAreaId());
		}
	}

	private void transferList(List<Area> areaList, List<ListVo> result) {
		for (Area item : areaList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getAreaId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 地域列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchAreaBo bo) {
		PageVo<ListAreaVo> result = new PageVo<ListAreaVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(areaDao.list(bo));
        result.setTotal(areaDao.countList(bo));
		return result;
	}
	
	/**
	 * 地域详情
	 * @param areaId
	 * @return
	 * @throws OceanException
	 */
	public Object detail(Long areaId) throws OceanException {
		Object result = areaDao.detail(areaId);
		if (result == null) {
			throw new OceanException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
